<template>
  <a-modal
    title="人员信息"
    :visible="visible"
    :confirm-loading="loading"
    @ok="handleOk"
    @cancel="handleCancel"
    :afterClose="afterClose"
  >
    <a-form-model ref="form" :model="form" :rules="rules" :labelCol="{span:4}" :wrapperCol="{span:20}">
      <a-form-model-item has-feedback label="昵称" prop="userName">
        <a-input v-model.trim="form.userName" autocomplete="off" />
      </a-form-model-item>
      <a-form-model-item has-feedback label="账号" prop="accountNumber">
        <a-input v-model.trim="form.accountNumber" autocomplete="off" />
      </a-form-model-item>
      <a-form-model-item has-feedback label="密码" prop="password" v-if="formData?false:true">
        <a-input v-model.trim="form.password" autocomplete="off" />
      </a-form-model-item>
      <a-form-model-item has-feedback label="厂区" prop="factoryId">
        <a-select v-model="form.factoryId" default-value="" @focus="getFactoryData">
          <a-select-option v-for="(item,index) in factorys" :key="index" :value="item.value">
            {{item.name}}
          </a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
  
</template>

<script>
import {getFactorysByName,savePerson} from "../../api/index";
export default {
  name:"PersonFormModal",
  props:{
    visible:Boolean,
    formData:Object
  },
  watch:{
    formData(newVal,oldVal){
      if(newVal&&newVal.id){
        this.form=newVal;
      }
    }
  },
  data(){
    const validatorPassword=(rule, value, callback) =>{
      const reg=/^(?=.*\d)(?=.*[a-z|A-Z]).{8,10}$/;
      if (!reg.test(value)) {
        return callback(new Error('必须包含字母和数字的组合,长度在8-10之间'));
      } 
      callback();
    }
    return {
      loading:false,
      form:{
        userName:"",
        accountNumber:"",
        factoryId:"",
        password:""
      },
      rules:{
        userName:[{ required: true, message: '请输入昵称', trigger: 'blur' },],
        accountNumber:[{ required: true, message: '请输入账号', trigger: 'blur' },],
        factoryId:[{ required: true, message: '请输入厂区', trigger: 'blur' },],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator:validatorPassword,  trigger: 'blur' },
        ]
      },
      factorys:[],
    }
  },
  created(){
    this.getFactoryData();
  },
  methods:{
    getFactoryData(){
      this.factorys=[]
      getFactorysByName().then(data =>{
        data.map(item =>{
          this.factorys.push({
            name:item.name,
            value:item.id
          })
        })
      })
    },
    afterClose(){
      this.$refs.form.resetFields();
      this.loading=false;
      this.$emit("update:formData",null);
      this.form={
        userName:"",
        accountNumber:"",
        factoryId:"",
        password:""
      };
    },
    handleCancel(){
      this.$emit("update:visible",false);
    },
    handleOk(){
      this.$refs.form.validate(valid =>{
        if(valid){ 
          this.loading=true;
          savePerson(this.form).then(res =>{
            this.loading=false;
            this.$emit("update:visible",false);
            this.$emit("ok");
            this.$message.success(this.form.id?'编辑成功':'添加成功');
          }).catch(err =>{
            this.loading=false;
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>